using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Masood.MobileGames.SlidingLib.Common;

namespace Masood.MobileGames.SlidingLib.Abstract
{
    public abstract class AbstractBox
    {
        #region Constructors
        public AbstractBox(int width, int height)
            : this(0, 0, width, height)
        {
        }

        public AbstractBox(float x, float y, int width, int height)
        {
            this.position = new Vector2(x, y);            
            this.width = width;
            this.height = height;

            this.boxAppearanceInfo = Helper.GetInstance().GetNextAppearnace();
        }

        public AbstractBox(ContentManager content, float x, float y, int width, int height)
            : this(x, y, width, height)
        {
            this.content = content;
            

        }
        #endregion

        protected BoxAppearanaceInfo boxAppearanceInfo;
        protected string text;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        protected Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        protected Texture2D texture2d = null;
        public Texture2D Texture
        {
            get { return texture2d; }
            set { texture2d = value; }
        }

        protected Microsoft.Xna.Framework.Content.ContentManager content;
        public ContentManager Content
        {
            set { content = value; }
        }

        //protected int widthOrHeight;
        //public int WidthOrHeight
        //{
        //    get { return widthOrHeight; }
        //}

        protected int width;
        public int Width
        {
            get { return width; }
        }

        protected int height;
        public int Height
        {
            get { return height; }
        }

        protected AbstractGameBoard board;
        public AbstractGameBoard GameBoard
        {
            get { return board; }
            set { board = value; }
        }

        public abstract void Draw(SpriteBatch spriteBatch);  

        public bool IsInRange(int clickedX, int clickedY)
        {
            int left = (int)position.X;
            int top = (int)position.Y;
            int right = left + this.width;
            int bottom = top + this.height;

            if ((clickedX >= left && clickedX <= right) &&
                (clickedY >= top && clickedY <= bottom))
                return true;

            return false;
        }

        public void Swap(AbstractBox emptyBox)
        {
            AbstractBox tempBox = this;

            //Same column
            if (emptyBox.Position.X == this.Position.X)
            {
                if ((this.Position.Y + this.height == emptyBox.Position.Y) ||
                    (emptyBox.Position.Y + emptyBox.height == this.Position.Y))
                {
                    SwapInSameColumn(emptyBox);
                    return;
                }
            }

            if (emptyBox.Position.Y == this.Position.Y) //Same row
            {
                if ((this.Position.X + this.width == emptyBox.Position.X) ||
                    (emptyBox.Position.X + emptyBox.width == this.Position.X))
                {
                    SwapInSameRow(emptyBox);
                    return;
                }
            }
        }

        private void SwapInSameColumn(AbstractBox emptyBox)
        {
            float tempY = this.Position.Y;
            //this.Button.Top = emptyBox.Y;
            this.position.Y = emptyBox.Position.Y;
            emptyBox.position.Y = tempY;
        }

        private void SwapInSameRow(AbstractBox emptyBox)
        {
            float tempX = this.position.X;
            //this.Button.Top = emptyBox.Y;
            this.position.X = emptyBox.position.X;
            emptyBox.position.X = tempX;
        }


        public bool CanSwap(AbstractBox emptyBox)
        {
            //Same column
            if (emptyBox.Position.X == this.Position.X)
            {
                if ((this.Position.Y + this.height == emptyBox.Position.Y) ||
                    (emptyBox.Position.Y + emptyBox.height == this.Position.Y))
                {
                    return true;
                }
            }

            if (emptyBox.Position.Y == this.Position.Y) //Same row
            {
                if ((this.Position.X + this.width == emptyBox.Position.X) ||
                    (emptyBox.Position.X + emptyBox.width == this.Position.X))
                {
                    return true;
                }
            }

            return false;
        }
    }
}
